package Touchy;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import project.V12;

/**
 * The GamePanel class contains all the game logic
 * It initiates the wordlist, and sets up the panels, textfield, and scorefield.
 * 
 * @author Sletten
 *
 */

@SuppressWarnings("serial")
public class GamePanel extends JPanel implements KeyListener
{
	private Touchy mTouchy;
	
	/**panel containing the textfield, score, and streak counter*/
	private JPanel mSouthPanel;
	/**panel showing the words in play*/
	private JPanel mCenterPanel;
	/**scoremeter at top of the tool*/
	private ScoreMeter mMeter;
	private JTextField mTextField;
	private JLabel mScoreField;
	private JLabel mStreakField;
	
	/**contains the words to be used*/
	private WordList mWordList;
	/**list of all words active*/
	private ArrayList<Word> mWords;
	/**list of words typed in*/
	private ArrayList<String> mRemove;
	
	private int mLevel;
	
	/**counts the correct words stream*/
	private int mStreak; 
	
	private int mScore;
	private int mYayScore;
	private int mBooScore;
	
	boolean mDone;
	
	// ceeps track of when to add new words and increase level
	double mStartTime;
	double mLastWordAddTime;
	double mLastLevelUp;
	
	GamePanel(Touchy inTouchy)
	{
		super();
		
		mTouchy = inTouchy;
		
		Timer.setTimerType(1); // sets timer type to 
		
		this.setPreferredSize(new Dimension(800,600));
		
		this.setLayout(new BorderLayout());
		
		mSouthPanel = new JPanel();
		mSouthPanel.setPreferredSize(new Dimension(800, 50));
		mSouthPanel.setBackground(Color.decode("#DEFFA1"));
		mSouthPanel.setLayout(null);
		this.add(mSouthPanel, BorderLayout.SOUTH);
		
		mCenterPanel = new JPanel();
		mCenterPanel.setPreferredSize(new Dimension(800, 530));
		mCenterPanel.setBackground(Color.decode("#BFD657"));
		mCenterPanel.setLayout(null);
		mCenterPanel.setVisible(true);
		this.add(mCenterPanel, BorderLayout.CENTER);
		
		
		mTextField = new JTextField(); // input textfield
		mTextField.setFont(new Font("Arial", Font.PLAIN, 22));
		mTextField.addKeyListener(this);
		mTextField.setText(" ");
		mSouthPanel.add(mTextField);
		mTextField.setBounds(250, 5, 300, 40);
		
		
		mMeter = new ScoreMeter(50);
		mMeter.setPreferredSize(new Dimension(800, 20));
		this.add(mMeter, BorderLayout.NORTH);
		
		
		mScoreField = new JLabel(); // Score field
		mScoreField.setFont(new Font("Arial", Font.PLAIN, 25));
		mSouthPanel.add(mScoreField);
		mScoreField.setBounds(20, 5, 230, 40);
		mScoreField.setText("Score: " + Integer.toString(mScore));
		
		mStreakField = new JLabel();
		mStreakField.setFont(new Font("Arial", Font.PLAIN, 25));
		mSouthPanel.add(mStreakField);
		mStreakField.setBounds(570, 5, 230, 40);
		mStreakField.setText("Streak: " + Integer.toString(mStreak));
		
		
		
		initGameStats();
	}
	
	private void initGameStats()
	{
		//init time
		mStartTime = Timer.getTime();
		mLastWordAddTime = mStartTime;
		mLastLevelUp = mStartTime;
		
		mWordList = new WordList();
		mWords = new ArrayList<Word>();
		mRemove = new ArrayList<String>();
		
		mMeter.setScore(80);
		
		mLevel = 2;
		
		mStreak = 0;
		
		mScore = 0;
		mYayScore = 10;
		mBooScore = -5;
	}
	
	public void run()
	{
		initGameStats();
		mDone = false;
		while(!mDone)
		{
			Timer.updateTimer();
			mTextField.requestFocus();
			
			// add new word
			if((mLastWordAddTime + 2) < Timer.getTime())
			{
				mLastWordAddTime = Timer.getTime();
				Word temp = new Word(mWordList.getWord(mLevel,mLevel+2));
				mWords.add(temp);
				mCenterPanel.add(temp);
				temp.init();
			}
			//increase level
			if((mLastLevelUp + 30) < Timer.getTime())
			{
				mLastLevelUp = Timer.getTime();
				mLevel++;
			}
			
			// while words have been entered, check if eaquals to words in play
			while (mRemove.size() > 0)
			{
				boolean found = false;
				for(int i = 0; i < mWords.size(); i++)
				{
					if(mRemove.get(0).equals(" " + mWords.get(i).getText()) ||
						mRemove.get(0).equals(mWords.get(i).getText()))
					{
						found = true;
						mCenterPanel.remove(mWords.get(i));
						mWords.remove(i);
					}
				}
				//if typed correct
				if(found)
				{
					mMeter.addScore();
					
					// maxed out meter?
					if(mMeter.getScore() == 100)
					{
						// bonus score :D:D
						mScore += 1.5*mYayScore;
					}
					else
					{
						mScore += mYayScore;
					}
					
					mStreak++;
					
					this.repaint();
				}
				else
				{
					mMeter.removeScore();
				}
				
				//remove entered word
				mRemove.remove(0);
			}

			// move words
			for(int i = 0; i < mWords.size(); i++)
			{
				if(mWords.get(i) != null)
				{
					mWords.get(i).move();
				}
					
				if(mWords.get(i) != null && mWords.get(i).getY() > 500)
				{
					mCenterPanel.remove(mWords.get(i));
					mWords.remove(i);
					
					mScore += mBooScore;
					mMeter.removeScore();
					mMeter.removeScore();
					mMeter.removeScore();
					mStreak = 0;
					
				}
			}
			
			mScoreField.setText("Score: " + Integer.toString(mScore));
			mStreakField.setText("Streak: " + Integer.toString(mStreak));
			
			//if lost
			if(mMeter.getScore() <= 0)
			{
				mTouchy.getEndGame().setScore(mScore);
				mTouchy.setState(mTouchy.ENDGAME);
				
				// clear the playfields
				mCenterPanel.removeAll();
				
				mDone = true;
			}
			
			this.repaint();
			mCenterPanel.repaint();
			V12.getInstance().update();
		}
	}
	

	
	@Override
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyChar() == ' ')
		{
			mRemove.add(mTextField.getText());
			
			mTextField.setText(null);
		}
		
	}
	@Override
	public void keyReleased(KeyEvent arg0) {
		
	}
	@Override
	public void keyTyped(KeyEvent arg0)
	{
	}
}